<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    class MyPromise {
      constructor(handle) {  // handle---->(resolve,reject)=>{}
        this['[[PromiseState]]'] = "pendding";
        this['[[PromiseResult]]'] = undefined;
        handle(this.#_resolve.bind(this), this.#_reject.bind(this));

      }
      #_resolve (val) {  // 私有方法 需要在前面加上#号，只能类的内部调用外部不能调用
        // 外部的resolve函数
        this['[[PromiseState]]'] = "pendding";
        this['[[PromiseResult]]'] = val;
        // 内部延迟执行
        setTimeout(() => {
          this.resolveFn(val); // 在调取 resolve的时候执行 保存的onResolved函数
        })
      }
      #_reject (err) {
        this['[[PromiseState]]'] = "pendding";
        this['[[PromiseResult]]'] = err;
        // 内部延迟执行
        setTimeout(() => {
          this.rejectFn(err);
        });
        setTimeout(() => {
          throw new Error(err) // 抛出错误
        })
      }

    }
  </script>
</body>

</html>